/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.fineract.portfolio.loanproduct;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import io.cucumber.java8.En;
import org.apache.commons.lang3.StringUtils;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProduct;
import org.apache.fineract.portfolio.loanproduct.domain.LoanProductTrancheDetails;
import org.apache.fineract.portfolio.loanproduct.exception.LoanProductGeneralRuleException;

public class LoanProductValidationStepDefinitions implements En {

    private LoanProduct loanProduct;

    private LoanProductGeneralRuleException ruleException;

    public LoanProductValidationStepDefinitions() {
        Given("/^A loan product with values `allowMultipleDisbursal` is (.*), `disallowExpectedDisbursements` is (.*), `allowApprovedDisbursedAmountsOverApplied` is (.*), `overAppliedCalculationType` is (.*) and `overAppliedNumber` is (.*)$/",
                (String allowMultipleDisbursal, String disallowExpectedDisbursements, String allowApprovedDisbursedAmountsOverApplied,
                        String overAppliedCalculationType, String overAppliedNumber) -> {
                    this.loanProduct = newLoanProduct(Boolean.parseBoolean(allowMultipleDisbursal));

                    if (!StringUtils.isBlank(disallowExpectedDisbursements)) {
                        loanProduct.setDisallowExpectedDisbursements(Boolean.parseBoolean(disallowExpectedDisbursements));
                    }
                    if (!StringUtils.isBlank(allowApprovedDisbursedAmountsOverApplied)) {
                        loanProduct.setAllowApprovedDisbursedAmountsOverApplied(
                                Boolean.parseBoolean(allowApprovedDisbursedAmountsOverApplied));
                    }
                    if (!StringUtils.isBlank(overAppliedCalculationType) && !"-".equals(overAppliedCalculationType)) {
                        loanProduct.setOverAppliedCalculationType(overAppliedCalculationType);
                    }
                    if (!StringUtils.isBlank(overAppliedNumber) && Integer.parseInt(overAppliedNumber) >= 0) {
                        loanProduct.setOverAppliedNumber(Integer.parseInt(overAppliedNumber));
                    }
                });

        When("/^The user validates the loan product before saving$/", () -> {
            this.ruleException = assertThrows(LoanProductGeneralRuleException.class, () -> loanProduct.validateLoanProductPreSave());
        });

        Then("/^An exception (.*) with message template (.*) should be thrown$/", (String exception, String messageTemplate) -> {
            if (!StringUtils.isBlank(exception)) {
                assertEquals(Class.forName(exception.trim()), this.ruleException.getClass());
            }
            if (!StringUtils.isBlank(messageTemplate)) {
                assertThat(messageTemplate.trim(), is(this.ruleException.getGlobalisationMessageCode()));
            }
        });
    }

    private LoanProduct newLoanProduct(boolean allowMultipleDisbursals) {
        LoanProduct lp = new LoanProduct();
        lp.setLoanProductTrancheDetails(new LoanProductTrancheDetails(allowMultipleDisbursals, null, null, false));
        return lp;
    }
}
